package com.example.smartcity_ikun3.ui.person.activity

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.model.LoginModel
import com.example.smartcity_ikun3.logic.network.RetrofitService.Companion.create
import com.example.smartcity_ikun3.logic.network.SmartApi
import com.example.smartcity_ikun3.logic.util.Util.show
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import com.example.smartcity_ikun3.ui.App.*
import kotlin.concurrent.thread

class LoginAddActivity : AppCompatActivity() {

    // 登录和注册切换
    private lateinit var mPersonOk: LinearLayout
    private lateinit var mPersonNo: LinearLayout
    private lateinit var mAddLoginNo: TextView
    private lateinit var mAddLoginOk: TextView

    // 点击登录
    private lateinit var mPersonName: EditText
    private lateinit var mPersonPassword: EditText
    private lateinit var mPersonBtnOk: Button

    // 注册
    private lateinit var mNoPersonName: EditText
    private lateinit var mNoPersonPassword: EditText
    private lateinit var mNoPersonUserName: EditText
    private lateinit var mNoPersonPhone: EditText
    private lateinit var mNoBtnOk: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_login_add)
        title = "登录"
        mPersonOk = findViewById(R.id.personLayoutOk)
        mPersonNo = findViewById(R.id.personLayoutNo)
        mAddLoginNo = findViewById(R.id.addLoginNo)
        mAddLoginOk = findViewById(R.id.addLoginOk)
        mPersonName = findViewById(R.id.personName)
        mPersonPassword = findViewById(R.id.personPassword)
        mPersonBtnOk = findViewById(R.id.personBtnOk)

        mNoPersonName = findViewById(R.id.personNameNo)
        mNoPersonPassword = findViewById(R.id.personPasswordNo)
        mNoPersonUserName = findViewById(R.id.personUserName)
        mNoPersonPhone = findViewById(R.id.personPhone)
        mNoBtnOk = findViewById(R.id.personBtnNo)




        setView()
        setEvent()

        // 点击登录
        setOk()

        // 点击注册
        setLoginAdd()
    }

    /**
     * 注册
     */
    private fun setLoginAdd() {
        mNoBtnOk.setOnClickListener {
            if (mNoPersonName.text.isEmpty() &&
                mNoPersonPassword.text.isEmpty() &&
                mNoPersonUserName.text.isEmpty() &&
                mNoPersonPhone.text.isEmpty()){
                "您还没输入完整哦！".show()
            }else {
                create<SmartApi>().getAddLogin(
                    RequestBody.create(
                        "application/json".toMediaTypeOrNull(),
                        JSONObject().apply {
                            put("userName", mNoPersonName.text)
                            put("password", mNoPersonPassword.text)
                            put("nickName", mNoPersonUserName.text)
                            put("phonenumber", mNoPersonPhone.text)
                        }.toString()
                    )
                ).enqueue(object : Callback<LoginModel>{
                    override fun onFailure(p0: Call<LoginModel>, p1: Throwable) {
                        "注册失败！".show()
                        Log.d("NET", "onFailure: loginAdd---> ${p1.message} ")
                    }

                    override fun onResponse(p0: Call<LoginModel>, p1: Response<LoginModel>) {
                        val body = p1.body()
                        if (body != null){
                            if (body.code == 200){
                                body.msg.show()
                                // 切换到登录界面
                                mPersonNo.visibility = View.GONE
                                mPersonOk.visibility = View.VISIBLE
                            }
                        }
                    }
                })
            }
        }

    }

    /**
     * 登录
     */
    private fun setOk() {
        mPersonBtnOk.setOnClickListener {
            if (mPersonName.text.isEmpty() && mPersonPassword.text.isEmpty()) {
                "账号或密码不能为空".show()
            }else{
                create<SmartApi>().postLogin(
                    RequestBody.create("application/json".toMediaTypeOrNull(), JSONObject().apply {
                        put("username", mPersonName.text.toString())
                        put("password", mPersonPassword.text.toString())
                    }.toString())
                ).enqueue(object : Callback<LoginModel>{
                    override fun onFailure(p0: Call<LoginModel>, p1: Throwable) {
                        "登录失败！".show()
                        Log.d("NET", "onFailure:  login ---> 登录失败！")
                    }

                    override fun onResponse(p0: Call<LoginModel>, p1: Response<LoginModel>) {
                        val body = p1.body()
                        if (body != null) {
                            if (body.code == 200){
                                runOnUiThread {
                                    getSharedPreferences("token", Context.MODE_PRIVATE).edit().apply {
                                        putBoolean("user", true)
                                        putString("token", body.token)
                                        apply()
                                    }
                                }
                                finish()
                                "登录成功".show()
                            }else body.msg.show()
                        }
                    }

                })
            }
        }
    }

    private fun setEvent() {
        // 登录和注册切换
        mAddLoginNo.setOnClickListener {
            mPersonOk.visibility = View.GONE
            mPersonNo.visibility = View.VISIBLE
        }

        mAddLoginOk.setOnClickListener {
            mPersonNo.visibility = View.GONE
            mPersonOk.visibility = View.VISIBLE
        }
    }

    private fun setView() {

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}